package com.wjy.common.util.redis;

import com.wjy.common.config.redis.RedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * @author ybxxszl
 * @desc Redis工具类
 * @date 2019/4/15
 */
@Component
public class RedisUtil {

    @Autowired
    private RedisConfig config;

    public void set(String key, String value, int seconds) {
        Jedis jedis = config.getJedis();
        jedis.set(key, value);
        jedis.expire(key, seconds);
        jedis.close();
    }

    public void set(String key, String value) {
        Jedis jedis = config.getJedis();
        jedis.set(key, value);
        jedis.close();
    }

    public int setnx(String key, String value) {
        Jedis jedis = config.getJedis();
        Long num = jedis.setnx(key, value);
        jedis.close();
        return num.intValue();
    }

    public String get(String key) {
        Jedis jedis = config.getJedis();
        String value = jedis.get(key);
        jedis.close();
        return value;
    }

    public void del(String key) {
        Jedis jedis = config.getJedis();
        jedis.del(key);
        jedis.close();
    }

    public int lpush(String key, String... values) {
        Jedis jedis = config.getJedis();
        Long num = jedis.lpush(key, values);
        jedis.close();
        return num.intValue();
    }

    public int rpush(String key, String... values) {
        Jedis jedis = config.getJedis();
        Long num = jedis.rpush(key, values);
        jedis.close();
        return num.intValue();
    }

    public String lpop(String key) {
        Jedis jedis = config.getJedis();
        String value = jedis.lpop(key);
        jedis.close();
        return value;
    }

    public String rpop(String key) {
        Jedis jedis = config.getJedis();
        String value = jedis.rpop(key);
        jedis.close();
        return value;
    }

    public List<String> blpop(int timeout, String... keys) {
        Jedis jedis = config.getJedis();
        List<String> value = jedis.blpop(timeout, keys);
        jedis.close();
        return value;
    }

    public List<String> brpop(int timeout, String... keys) {
        Jedis jedis = config.getJedis();
        List<String> value = jedis.brpop(timeout, keys);
        jedis.close();
        return value;
    }

}
